package tt.com.serviceprojectmanagement.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tt.com.common.enums.Status;
import tt.com.common.model.YxCommonServeProviders;
import tt.com.common.model.YxCommonServeType;
import tt.com.common.util.CommonResult;
import tt.com.serviceprojectmanagement.service.TypeDateListService;

import java.util.List;

/**
 * @author BEAN
 * @version 2020
 * @date 2022/8/31 11:39
 */

@RestController
@RequestMapping("serve/type")
public class TypeDateListController {

    @Autowired
    private TypeDateListService typeDateListService;

    /**
     * 逻辑删除
     * @param yxCommonServeType
     * @return
     */
    @PostMapping("/del")
    public CommonResult delTypeDateById(@RequestBody YxCommonServeType yxCommonServeType){
        Boolean aBoolean = typeDateListService.delTypeDate(yxCommonServeType.getId(), yxCommonServeType.getDeleteUserId());
        if (!aBoolean){
            return CommonResult.buildR(Status.DEFAULT, "该条数据已删除或不存在");
        }
        return CommonResult.buildR(Status.OK,"删除成功！");
    }

    /**
     * 根据id更新分类名称
     * @param id
     * @param newName
     * @return
     */
    @GetMapping("/updateName")
    public CommonResult updateTypeNameById(@RequestParam(required = true) String id, @RequestParam(required = true) String newName){
        Boolean aBoolean = typeDateListService.updateTypeName(id, newName);
        if (!aBoolean){
            return CommonResult.buildR(Status.DEFAULT, "更新失败,该数据不存在");
        }
        return CommonResult.buildR(Status.OK,"更新成功！");
    }

    /**
     * 服务项目分类新增
     * @param serveType
     * @return
     */
    @PostMapping
    public CommonResult insertNewType(@RequestBody YxCommonServeType serveType){
        Integer integer = typeDateListService.insertNewType(serveType);
        if (integer == 2 || integer <= 0){
            return CommonResult.buildR(Status.DEFAULT, "新增失败，该数据已存在");
        }
        return CommonResult.buildR(Status.OK,"新增成功！");
    }

    /**
     * 查询服务类型下的所有服务项目
     * @param serveId
     * @return
     */
    @GetMapping("/searchAll/Providers")
    public CommonResult searchAllProviders(@RequestParam(required = true) String serveId){
        List<YxCommonServeProviders> list = typeDateListService.searchAll(serveId);
        if (list.size() <= 0 || list == null){
            return CommonResult.buildR(Status.DEFAULT, "查无数据");
        }

        return CommonResult.buildR(Status.OK,"查询成功！", list);
    }


    /**
     * 查询所有一级分类
     * @return
     */
    @PostMapping("/searchAll/firstType")
    public CommonResult searchAllFirstType(){
        List<YxCommonServeType> list = typeDateListService.searchAllType();
        if (list.size() <= 0 || list == null){
            return CommonResult.buildR(Status.DEFAULT, "查无数据");
        }

        return CommonResult.buildR(Status.OK, list);
    }

}
